﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Solution.Application.Commands.Dtos;
using System;
using System.Linq;

namespace Solution.Application.Injection
{
    public class RequestFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var result = new HandlerResult<string>()
                {
                    code = ResultCode.StatusOK,
                };

                foreach (var item in context.ModelState.Values)
                {
                    result.msg = String.Join(",", item.Errors.Select(p => p.ErrorMessage));
                }

                context.Result = new JsonResult(result);
            }
        }
    }
}